home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / PGM_TOOL / PCKSELFM / PCKSELFM.PAS next >
Pascal/Delphi Source File  |  1991-01-18  |  3KB  |  115 lines

  1. Unit PCkSelfM;
  2.  
  3. {
  4.  
  5.  Programmer: Jim Nicholson
  6.  
  7.  Purpose: Implement a method for creating "self-modifying" .EXE files from
  8.  TP which will survive the encoding techniques used by LZEXE and PKLite(tm).
  9.  
  10.  For discussion and examples, see SelfMod.Pas
  11.  
  12.  This unit contains code placed into the public domain, with the following
  13.  provision:
  14.  
  15.  Please do not distribute modified versions of this code without indicating
  16.  such modification by commenting the file.
  17.  
  18.  If you have questions, comments, modifications, or suggestions, please
  19.  feel free to contact us:
  20.  
  21.               PCkS Associates
  22.               138 Frances Place
  23.               Hillside, NJ 07205
  24.  
  25.               On CompuServe, EasyPlex to    70152,332
  26.               On Delphi                     CHICKENJN
  27.               On GENie                      J.NICHOLSON1
  28.  
  29. }
  30.  
  31. interface
  32.  
  33. var
  34.     ExeFileName : string[128];
  35.  
  36. function  ConfigBlockPresent(Size : integer)          : boolean;
  37. function  NewConfigBlock(var C_B; Size : integer)     : boolean;
  38. function  ReadConfigBlock(var C_B; Size : integer)    : boolean;
  39. function  ConfigBlockRewrite(var C_B; Size : integer) : boolean;
  40.  
  41. implementation
  42.  
  43. Uses Dos;
  44.  
  45. const
  46.     SelfModHeader : string[10] = 'PCkS SMODF';
  47.     CtrlZ : char = ^Z;
  48. var
  49.     ExeFile: file;
  50.     Buffer : string[10];
  51.  
  52. function ConfigBlockPresent(Size : integer) : boolean;
  53.  begin
  54.  assign(ExeFile,ExeFileName);
  55.  reset(ExeFile,1);
  56.  seek(ExeFile,
  57.       FileSize(ExeFile)
  58.       -(SizeOf(SelfModHeader)+Size+1));
  59.  BlockRead(ExeFile,Buffer,SizeOf(SelfModHeader));
  60.  if Buffer = SelfModHeader then
  61.     ConfigBlockPresent := true
  62.  else
  63.     ConfigBlockPresent := false;
  64.  close(ExeFile);
  65.  end;
  66.  
  67. function NewConfigBlock(var C_B; Size : integer) : boolean;
  68.  begin
  69.  NewConfigBlock := false;
  70.  if not ConfigBlockPresent(Size) then begin
  71.      assign(ExeFile,ExeFileName);
  72.      reset(ExeFile,1);
  73.      Seek(ExeFile,FileSize(ExeFile));
  74.      BlockWrite(ExeFile,SelfModHeader,SizeOf(SelfModHeader));
  75.      BlockWrite(ExeFile,C_B,Size);
  76.      BlockWrite(ExeFile,CtrlZ,1);
  77.      close(ExeFile);
  78.      NewConfigBlock := true;
  79.      end;
  80.  end;
  81.  
  82. function ReadConfigBlock(var C_B; Size : integer) : boolean;
  83.  begin
  84.  ReadConfigBlock := false;
  85.  if ConfigBlockPresent(Size) then begin
  86.      assign(ExeFile,ExeFileName);
  87.      reset(ExeFile,1);
  88.      seek(ExeFile,FileSize(ExeFile)-(Size+1));
  89.      BlockRead(ExeFile,C_B,Size);
  90.      close(ExeFile);
  91.      ReadConfigBlock := true;
  92.      end;
  93.  end;
  94.  
  95. function ConfigBlockRewrite(var C_B; Size : integer) : boolean;
  96.  var
  97.      Temp : string;
  98.  begin
  99.  ConfigBlockRewrite := false;
  100.  if ConfigBlockPresent(Size) then begin
  101.      assign(ExeFile,ExeFileName);
  102.      reset(ExeFile,1);
  103.      seek(ExeFile,FileSize(ExeFile)-(SizeOf(SelfModHeader)+Size+1));
  104.      BlockWrite(ExeFile,SelfModHeader,SizeOf(SelfModHeader));
  105.      BlockWrite(ExeFile,C_B,Size);
  106.      BlockWrite(ExeFile,CtrlZ,1);
  107.      close(ExeFile);
  108.      ConfigBlockRewrite := true;
  109.      end;
  110.  end;
  111.  
  112. begin
  113. ExeFileName := ParamStr(0);
  114. end.
  115.